/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.service.factory.impl;

import com.playground.core.entity.GameTitle;
import com.playground.core.entity.LoggedPlayer;
import com.playground.core.entity.boardgame.BoardGame;
import com.playground.core.entity.boardgame.Piece;
import com.playground.core.entity.boardgame.chesslikegame.checkers.CheckersBoard;
import com.playground.core.entity.boardgame.chesslikegame.checkers.CheckersPiece;
import com.playground.core.entity.boardgame.tictactoe.TicTacToeBoard;
import com.playground.core.repository.GameTitleRepository;
import com.playground.core.service.factory.GameFactory;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.inject.Inject;
import org.springframework.stereotype.Service;

/**
 *
 * @author Tiago Peres
 */
@Service("gameFactory")
public class DefaultGameFactory implements GameFactory {
    @Inject
    private GameTitleRepository gameTitleRepository;
    
    @Override
    public BoardGame createCheckers(List<LoggedPlayer> players){
        BoardGame checkers = new BoardGame();
        checkers.setPlayers(players);
        GameTitle title = gameTitleRepository.findByName("Checkers");
        if (null != title){
            checkers.setGameTitle(title);
        }
        // rearange list of players
        Random rand = new Random();
        rand.setSeed(new Date().getTime());
        int startingPlayerIndex = Math.abs(rand.nextInt() % 2);
        int otherPlayerIndex = (startingPlayerIndex == 0) ? 1 : 0;
        LoggedPlayer startingPlayer = checkers.getPlayers().get(startingPlayerIndex);
        LoggedPlayer otherPlayer = checkers.getPlayers().get(otherPlayerIndex);
        checkers.getPlayers().remove(1);
        checkers.getPlayers().remove(0);
        checkers.getPlayers().add(startingPlayer);
        checkers.getPlayers().add(otherPlayer);
        
        CheckersBoard board = new CheckersBoard();
        
        // generate player 0 pieces
        int i = 0, j = 0;
        for (int k = 0; k < 12; k++){
            Piece p = new CheckersPiece(k);
            p.setOwner(checkers.getPlayers().get(0));
            board.addPieceAt(p, board.getSquareAt(i, j));
            j += 2;
            if (j > 7){
                i++;
                j = 1 - (j % 8);
            }
        }
        
        // generate player 1 pieces
        i = 7;
        j = 7;
        for (int k = 12; k < 24; k++){
            Piece p = new CheckersPiece(k);
            p.setOwner(checkers.getPlayers().get(1));
            board.addPieceAt(p, board.getSquareAt(i, j));
            j -= 2;
            if (j < 0){
                i--;
                j = 7 + (j % 2);
            }
        }
        
        checkers.setBoard(board);
        checkers.setPlayerInTurn(startingPlayer);
        checkers.setWinner(null);
        checkers.setNumberOfMoves(0);
        
        return checkers;
    }

    public BoardGame createTicTacToe(List<LoggedPlayer> players) {
        
        BoardGame ticTacToe = new BoardGame();
        ticTacToe.setPlayers(players);
        GameTitle title = gameTitleRepository.findByName("Checkers");
        if (null != title){
            ticTacToe.setGameTitle(title);
        }
        // rearange list of players
        Random rand = new Random();
        rand.setSeed(new Date().getTime());
        int startingPlayerIndex = Math.abs(rand.nextInt() % 2);
        int otherPlayerIndex = (startingPlayerIndex == 0) ? 1 : 0;
        LoggedPlayer startingPlayer = ticTacToe.getPlayers().get(startingPlayerIndex);
        LoggedPlayer otherPlayer = ticTacToe.getPlayers().get(otherPlayerIndex);
        ticTacToe.getPlayers().remove(1);
        ticTacToe.getPlayers().remove(0);
        ticTacToe.getPlayers().add(startingPlayer);
        ticTacToe.getPlayers().add(otherPlayer);
        
        TicTacToeBoard board = new TicTacToeBoard();
        
        ticTacToe.setBoard(board);
        ticTacToe.setPlayerInTurn(startingPlayer);
        ticTacToe.setWinner(null);
        
        return ticTacToe;
    }
    
}
